from utils import json_util, statisticalutil, parse_util


#   b : bool 布尔
#   n : int 数量
#   k : dict K线
#   v : int 数值
#   x,y,z: list 数组
#   CROSS(x,y)  交叉
#   CROSS_UP(x,y)  交叉
#   CROSS_DOWN(x,y)  交叉
#   OVER(x,y,N) 凌驾
#   BELOW(x,y,N) 低于
#   MORE(x,V,N) 大于
#   LESS(x,V,N) 小于
#   AND(b,b) 小于
#   OR(b,b) 小于
#   CORRELATION(k1,k2) 关联度
#   AVE(x) 平均值


# 关联度
def CORRELATION_K(k1: list, k2: list) -> float:
    return statisticalutil.mean(
        [statisticalutil.correlation(parse_util.list_dict_to_list(k1, 'kp'), parse_util.list_dict_to_list(k2, 'kp')),
         statisticalutil.correlation(parse_util.list_dict_to_list(k1, 'sp'), parse_util.list_dict_to_list(k2, 'sp')),
         statisticalutil.correlation(parse_util.list_dict_to_list(k1, 'min'), parse_util.list_dict_to_list(k2, 'min')),
         statisticalutil.correlation(parse_util.list_dict_to_list(k1, 'max'), parse_util.list_dict_to_list(k2, 'max'))])


def MORE(x: list, v: int, n: int):
    x_len = len(x)
    if x_len < n:
        return False
    for i in range(n):
        if x[x_len - i - 1] >= v:
            continue
        return False
    return True


def LESS(x: list, v: int, n: int):
    x_len = len(x)
    if x_len < n:
        return False
    for i in range(n):
        if x[x_len - i - 1] <= v:
            continue
        return False
    return True


# 完全高于
# x >= y
# n 低于次数
def OVER(x: list, y: list, n: int) -> bool:
    x_len = len(x)
    y_len = len(y)
    if x_len < n or y_len < n:
        return False
    for i in range(n):
        if x[x_len - i - 1] >= y[y_len - i - 1]:
            continue
        return False
    return True

    # x = {'key': rq, 'value': y} x日期从小到大


# 完全低于
# n 低于次数
def BELOW(x: list, y: list, n: int) -> bool:
    return OVER(y, x, n)


def parse_x(ks, key, value):
    x = []
    for k in ks:
        k = json_util.to_json(k)
        x.append({'key': k[key], 'value': float(k[value])})
    return x


def parse_a(ks, key):
    a = []
    for k in ks:
        k = json_util.to_json(k)
        a.append(k[key])
    return a


# 给a赋值
def assign_key(x: list, key, a: list):
    y = []
    for i in range(len(x)):
        y.append({'key': x[i].get(key), 'value': a[i]})
    return y


# 移动平均线
# x: {'key':'' ,'value':''}
def AVE(x: list, n: int):
    r = []
    sum_n = 0
    for i in range(n):
        sum_n += x[i].get('value')
        r.append({'key': x[i].get('key'), 'value': round(sum_n / (i + 1), 4)})
    for i in range(n, len(x)):
        sum_n = sum_n + x[i].get('value') - x[i - n].get('value')
        r.append({'key': x[i].get('key'), 'value': round(sum_n / n, 4)})
    return r


# 5点3次平滑曲线
# a : [] , 全数字
def MEAN53(a: list):
    n = len(a)
    b = [0 for _ in range(n)]
    for i in range(n):
        pass  # 赋值语句
    for k in range(n):
        b[0] = (69 * a[0] + 4 * (a[1] + a[3]) - 6 * a[2] - a[4]) / 70
        b[1] = (2 * (a[0] + a[4]) + 27 * a[1] + 12 * a[2] - 8 * a[3]) / 35
        for j in range(n - 2):
            b[j] = (-3 * (a[j - 2] + a[j + 2]) + 12 * (a[j - 1] + a[j + 1]) + 17 * a[j]) / 35
        b[n - 2] = (2 * (a[n - 1] + a[n - 5]) + 27 * a[n - 2] + 12 * a[n - 3] - 8 * a[n - 4]) / 35
        b[n - 1] = (69 * a[n - 1] + 4 * (a[n - 2] + a[n - 4]) - 6 * a[n - 3] - a[n - 5]) / 70
    return b


# 表示当A从下方向上穿过B时返回1,否则返回0
# a:list
# b:list
# 返回参数上传点和标识
def CROSS(x: list, y: list):
    result_list = []
    # 数据验证
    av0 = json_util.to_json(x[0])
    bv0 = json_util.to_json(y[0])
    if av0['key'] != bv0['key'] or len(x) != len(y):
        print('表示当A从下方向上穿过B时返回1,否则返回0：数据格式错误')
        return '数据格式错误'
    if av0['value'] < bv0['value']:
        flag = -1
    else:
        flag = 1
    for i in range(len(x)):
        avi = json_util.to_json(x[i])
        bvi = json_util.to_json(y[i])
        if avi['value'] >= bvi['value'] and flag == -1:
            result_list.append({'key': avi['key'], 'value': 1})
            flag = 1
            continue
        elif avi['value'] < bvi['value'] and flag == 1:
            flag = -1
        result_list.append({'key': avi['key'], 'value': 0})
    return result_list


# n天前的值
def REF(x, n):
    len_num = len(x)
    ref_list = []
    for i in range(n):
        ref_list.append({'key': x[i]['key'], 'value': 0})
    for i in range(n, len_num):
        ref_list.append({'key': x[i]['key'], 'value': x[i - n]['value']})
    return ref_list


# 同花顺 LLV
# x = {'key': rq, 'value': y} x日期从小到大
# 1 2 3 4 5 6 7 8 9 10 11 12 13
def LLV(x, n):
    print('llv')
    llv_list = []
    llv_list.append(x[0])
    min_num = 0
    min_value = x[0]['value']
    for i in range(1, len(x)):
        a = json_util.to_json(x[i])
        size = min(i + 1, n)
        if (i - size) == min_num:
            min_value = a['value']
            min_num = i
            for j in range(1, size):
                b = json_util.to_json(x[i - j])
                if min_value > b['value']:
                    min_value = b['value']
                    min_num = i - j
        else:
            if min_value > a['value']:
                min_value = a['value']
                min_num = i
        llv_list.append({'key': a['key'], 'value': min_value})
    return llv_list


# 同花顺 HHV
def HHV(x, n):
    print('HHV')
    hhv_list = []
    hhv_list.append(x[0])
    max_num = 0
    max_value = x[0]['value']
    for i in range(1, len(x)):
        a = json_util.to_json(x[i])
        size = min(i + 1, n)
        if (i - size) == max_num:
            max_value = a['value']
            max_num = i
            for j in range(1, size):
                b = json_util.to_json(x[i - j])
                if max_value < b['value']:
                    max_value = b['value']
                    max_num = i - j
        else:
            if max_value < a['value']:
                max_value = a['value']
                max_num = i
        hhv_list.append({'key': a['key'], 'value': max_value})
    return hhv_list


# 同花顺 SMA
# n 天的平滑曲线
# m 权重
def SMA(x, n, m):
    print('SMA')
    sma_list = []
    x0 = json_util.to_json(x[0])
    y = float(x0['value'])
    rq0 = x0['key']
    sma_info = {'key': rq0, 'value': y}
    sma_list.append(sma_info)
    daks_len = len(x)
    for i in range(1, daks_len):
        xi = json_util.to_json(x[i])
        sp = float(xi['value'])
        rq = xi['key']
        y = y + (sp - y) * m / n
        sma_list.append({'key': rq, 'value': y})
    # sma_list 日期从小到大
    # Y = [mX + (n - m)Y"]/n
    return sma_list


# 同花顺 EMA
def EMA(x, n):
    print('EMA')
    ema_list = []
    x0 = json_util.to_json(x[0])
    y = float(x0['value'])
    rq0 = x0['key']
    ema_info = {'key': rq0, 'value': y}
    ema_list.append(ema_info)
    daks_len = len(x)
    for i in range(1, daks_len):
        xi = json_util.to_json(x[i])
        sp = float(xi['value'])
        rq = xi['key']
        y = y + (sp - y) * 2 / n
        # EMA12 = EMA12 + (sp - EMA12) * 2 / 13
        ema_list.append({'key': rq, 'value': y})
    # ［2 * X + (N - 1) * Y’］ / (N + 1)
    return ema_list


# 相关系数
def CORRELATION(x, y):
    return statisticalutil.correlation(parse_util.list_dict_to_list(x, 'value'),
                                       parse_util.list_dict_to_list(y, 'value'))


if __name__ == '__main__':
    # ks = k_util.day_k_local('002959', 10)
    # x = parse_x(ks, 'rq', 'min')
    # llv_list = HHV(x, 2)
    # ks_list = []
    # llv = []
    # for i in range(10):
    #     ks_list.append(float(ks[i]['min']))
    #     llv.append(llv_list[i]['value'])
    # print(ks_list)
    # print(llv)
    print('')
